unit UDMP_FAT;

interface

uses
  SysUtils, Classes, DB, ADODB, Graphics, Controls, Forms, Dialogs, IniFiles, WinSock;

Const constDB_FILE_CONFIG     = 'CRWeb.udl';
      constDiretorioTXT_ARQ_RETORNO_TIPO1 = 'F:\SISTEMAS\CRWEB\TXT_ARQ-RETORNO_TIPO1\';
      constDiretorioTXT_ARQ_RETORNO_TIPO2 = 'F:\SISTEMAS\CRWEB\TXT_ARQ-RETORNO_TIPO2\';
      constDiretorioTXT_ARQ_RETORNO_TIPO3 = 'F:\SISTEMAS\CRWEB\TXT_ARQ-RETORNO_TIPO3\';

type
  TDataModuleX = class(TDataModule)
    ADOConn: TADOConnection;
    ADOQryUsuario: TADOQuery;
    procedure DataModuleCreate(Sender: TObject);
    procedure DataModuleDestroy(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    function ValidaConexaoUDL(sLogin_CRWeb: String=''): Boolean;
    function GetHostUserName(): String; overload;
    function GetHostUserName(const Value: String): String; overload;
  end;

var
  DataModuleX: TDataModuleX;

implementation

uses UObjFunction;

{$R *.dfm}

procedure TDataModuleX.DataModuleCreate(Sender: TObject);
  var
    ix: Integer;
begin
  for ix := 0 to DataModuleX.ComponentCount - 1 do
  begin
    if(DataModuleX.Components[ix] IS TADOTable)then
    begin
      TADOTable(DataModuleX.Components[ix]).CommandTimeout := 60000;
    end;

    if((DataModuleX.Components[ix] IS TADOTable) And (TADOTable(DataModuleX.Components[ix]).Active))then
    begin
      TADOTable(DataModuleX.Components[ix]).Close;
    end;

    if((DataModuleX.Components[ix] IS TADOQuery) And (TADOQuery(DataModuleX.Components[ix]).Active))then
    begin
      TADOQuery(DataModuleX.Components[ix]).Close;
    end;
  end; // fim for

  DataModuleX.ADOConn.Close;
end;

procedure TDataModuleX.DataModuleDestroy(Sender: TObject);
  var
    ix: Integer;
begin
  for ix := 0 to DataModuleX.ComponentCount - 1 do
  begin
    if((DataModuleX.Components[ix] IS TADOTable) And (TADOTable(DataModuleX.Components[ix]).Active))then
    begin
      TADOTable(DataModuleX.Components[ix]).Close;
    end;

    if((DataModuleX.Components[ix] IS TADOQuery) And (TADOQuery(DataModuleX.Components[ix]).Active))then
    begin
      TADOQuery(DataModuleX.Components[ix]).Close;
    end;
  end; // fim for

  DataModuleX.ADOConn.Close;

end;

function TDataModuleX.GetHostUserName: String;
  var
    WSAData: TWSAData;   // uses WinSock
    HostEnt: PHostEnt;
    sComputer, sIPAddress: String;
begin
  Result:='';
  try
    WSAStartup(2, WSAData);
    SetLength(sComputer, 255);
    Gethostname(PChar(sComputer), 255);
    SetLength(sComputer, StrLen(PChar(sComputer)));
    HostEnt    := GetHostByName(PChar(sComputer));
    sIPAddress := Format('%3d.%3d.%3d.%3d',[Byte(HostEnt^.h_addr^[0]),Byte(HostEnt^.h_addr^[1]),Byte(HostEnt^.h_addr^[2]),Byte(HostEnt^.h_addr^[3])]);
    sIPAddress := StringReplace(sIPAddress, '.  1', '.1',[rfReplaceAll, rfIgnoreCase]);
    sComputer  := UpperCase(Trim(sComputer));
    WSACleanup;
    Result:=sComputer+'/'+Trim(sIPAddress);
  except
    On E:Exception Do
    Begin
      WSACleanup;
      ShowMessage('TDataModuleX - [GetHostUserName]'+#13+#13 + E.Message);
    end;
  end;

end;

function TDataModuleX.GetHostUserName(const Value: String): String;
  var
    ix: Integer;
begin
  if(((Value = 'MachineName') or (Value = 'Machine Name') or (Value = 'Name')) and (Value <> ''))then
  begin
    Result := Copy(Self.GetHostUserName, 1, Pos('/', Self.GetHostUserName) - 1);
  end
  else
  begin
    if(((Value = 'MachineIP') or (Value = 'Machine IP') or (Value = 'IP')) and (Value <> ''))then
    begin
      Result := Copy(Self.GetHostUserName, Pos('/', Self.GetHostUserName) + 1, Length(Self.GetHostUserName));
    end
    else
    begin
      Result := 'Error';
    end;
  end;
end;

function TDataModuleX.ValidaConexaoUDL(sLogin_CRWeb: String): Boolean;
  var PathFileConfig, Aux1: string;
  bx: Boolean;
  ObjFunc: TObjFunction;
begin
  bx := True;

  ObjFunc := TObjFunction.Create;
  sLogin_CRWeb := ObjFunc.GetVariavelDOS('USERNAME') + '/' + sLogin_CRWeb;

  ShowMessage(sLogin_CRWeb);

  PathFileConfig := 'FILE NAME=' + TRIMRIGHT(ExtractFilePath(Application.ExeName)) + constDB_FILE_CONFIG + ';APPLICATION NAME=' + sLogin_CRWeb;
  PathFileConfig := 'Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=CRWEB_TESTE;Data Source=192.168.1.161';
  PathFileConfig := 'Provider=SQLOLEDB.1;Password=123;Persist Security Info=True;User ID=sa;Initial Catalog=CRWEB_TESTE;Data Source=192.168.1.161';
  ShowMessage(PathFileConfig);
  try
    ADOConn.Close;
    ADOConn.ConnectionString := PathFileConfig;
    ADOConn.CommandTimeout := 60000;
    ADOConn.ConnectionTimeout := 60000;
    ADOConn.LoginPrompt := False;
    ADOConn.KeepConnection := True;
    try
      ADOConn.Connected := True;
    except
      on Erro: Exception do
      begin
        Aux1 := Erro.Message;
        bx := False;
        Result := bx;
        Exit;
      end;
    end; // fim try except
  finally
    if(Not bx)then
    begin
      ShowMessage('Erro em [ValidaConexaoUDL]' + #13 + #13 + #13 + 'Erro: ' + Aux1);
    end;
    Result := bx;
  end; // fim try finally
end;

end.
